﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// 声音管理器
    /// </summary>
    public class AudioManager : MonoSingleton<AudioManager>
    {
        /// <summary>
        /// 播放器池
        /// </summary>
        public List<AudioPlayer> PlayerList { get; private set; }
        /// <summary>
        /// 音轨字典
        /// </summary>
        public Dictionary<string, AudioTrackInfo> TrackDict { get; private set; }
        /// <summary>
        /// 声音目录字典
        /// </summary>
        public Dictionary<string, AudioClipCatalog> CatalogDict { get; private set; }
        /// <summary>
        /// 音轨音量变化事件
        /// </summary>
        public event Action<AudioTrackInfo> OnTrackVolumeChanged;
        public override void Init()
        {
            PlayerList = new List<AudioPlayer>();
            TrackDict = new Dictionary<string, AudioTrackInfo>();
            CatalogDict = new Dictionary<string, AudioClipCatalog>();
            Debug.Log("[LitAudio] 初始化完成");
        }

        #region 音量相关
        public void MuteAudio(bool mute)
        {

        }
        #endregion

        #region 播放器操作
        private AudioPlayer GetAudioPlayer(AudioTrackInfo track)
        {
            for (int i = 0; i < PlayerList.Count; i++)
            {
                if (!PlayerList[i].IsPlaying)
                {
                    PlayerList[i].Init(track);
                    return PlayerList[i];
                }
            }
            var newPlayer = CrateAudioPlayer();
            newPlayer.Init(track);
            PlayerList.Add(newPlayer);
            return newPlayer;
        }

        private AudioPlayer CrateAudioPlayer()
        {
            GameObject go = new GameObject("AudioPlayer");
            go.transform.SetParent(transform);
            go.AddComponent<AudioSource>();
            return go.AddComponent<AudioPlayer>();
        }
        #endregion

        #region 声音操作

        public AudioPlayer PlayAudio(AudioPlayInfo playInfo)
        {
            //检测信息是否完整
            var catalog = GetCatalog(playInfo.catalogId);
            if (catalog == null) return null;
            var audioInfo = catalog.GetAudioClipInfoById(playInfo.clipId);
            if (audioInfo == null) return null;
            var track = GetTrack(playInfo.trackId);
            if (track == null) return null;
            //使用播放器播放声音
            //TODO:细化
            var player = GetAudioPlayer(track);
            player.PlayAudio(audioInfo.auidoClip,1);
            return player;
        }

        public AudioPlayer PlayAudioFollowGameObject(AudioPlayInfo playInfo, GameObject go)
        {
            return null;
        }
        #endregion

        #region 音轨操作
        /// <summary>
        /// 设置音轨音量
        /// </summary>
        /// <param name="trackName"></param>
        /// <param name="volume"></param>
        public void SetTrackVolume(string Id, float volume)
        {
            if (!TrackDict.ContainsKey(Id))
            {
                Debug.LogError($"[LitAudio] Id为{Id}的音轨不存在");
                return;
            }
            TrackDict[Id].volume = volume;
            OnTrackVolumeChanged?.Invoke(TrackDict[Id]);
        }
        /// <summary>
        /// 获取音轨音量
        /// </summary>
        /// <param name="trackName"></param>
        /// <returns></returns>
        public float GetTrackVolume(string Id)
        {
            if (TrackDict.ContainsKey(Id))
                return TrackDict[Id].volume;
            Debug.LogError($"[LitAudio] 名字为{Id}的音轨不存在");
            return 0;
        }


        public void AddTrack(AudioTrackInfo track)
        {
            if (TrackDict == null)
                TrackDict = new Dictionary<string, AudioTrackInfo>();
            if (TrackDict.ContainsKey(track.Id))
            {
                Debug.LogError("[LitAudio] 添加音轨失败，音轨的Id必须保持唯一");
                return;
            }
            TrackDict.Add(track.Id, track);
        }

        public AudioTrackInfo GetTrack(string Id)
        {
            if (!TrackDict.ContainsKey(Id))
            {
                Debug.LogError($"[LitAudio] 不存在Id为{Id}的音轨");
                return null;
            }
            return TrackDict[Id];
        }
        #endregion

        #region 声音目录操作
        public void AddCatalog(AudioClipCatalog catalog)
        {
            if (CatalogDict == null)
                CatalogDict = new Dictionary<string, AudioClipCatalog>();
            CatalogDict.Add(catalog.Id, catalog);
        }
        public AudioClipCatalog GetCatalog(string Id)
        {
            if (!CatalogDict.ContainsKey(Id))
            {
                Debug.LogError("[LitAudio] 不存在Id为{Id}的声音目录");
                return null;
            }
            return CatalogDict[Id];
        }
        #endregion
    }

    /// <summary>
    /// 播放信息
    /// </summary>
    public class AudioPlayInfo
    {
        public string catalogId;
        public string clipId;
        public string trackId;
    }
}